import * as request from '../constant/fetchRequest'
import * as types from '../constant/action-types'
import HTTP from '../constant/httpCode'
import { message } from 'antd'
import { convertContent } from '../constant/lang-util'

export const searchStudentsByProgramId = (programId) => {
  return (dispatch) => {
    (async () => {
      const res = await request.get(`./api/programs/${programId}/students`)
      if (res.status === HTTP.OK) {
        dispatch({
          type: types.REFRESH_STUDENTS,
          data: res.body
        })
        dispatch(searchTutorsByProgramId(programId))
      }
    })()
  }
}
export const getGroups = () => {
  return (dispatch) => {
    (async () => {
      const res = await request.get(`./api/programs/groups`)
      if (res.status === HTTP.OK) {
        dispatch({
          type: types.REFRESH_TAGS,
          data: res.body
        })
      }
    })()
  }
}

export const editStudentGroups = (programId, studentId, groups) => {
  return (dispatch) => {
    (async () => {
      const res = await request.update(`./api/programs/${programId}/students/${studentId}/groups`, {groups})
      if (res.status === HTTP.NO_CONTENT) {
        message.success(convertContent('编辑成功'))
        dispatch(searchStudentsByProgramId(programId))
      }
    })()
  }
}

export const searchTutorsByProgramId = (programId) => {
  return (dispatch) => {
    (async () => {
      const res = await request.get(`./api/programs/${programId}/tutors`)
      if (res.status === HTTP.OK) {
        dispatch({
          type: types.REFRESH_TUTORS,
          data: res.body
        })
      }
    })()
  }
}

export const getMyStudents = (tutorId, programId) => {
  return (dispatch) => {
    (async () => {
      const res = await request.get(`./api/programs/${programId}/tutors/${tutorId}/students`)
      if (res.status === HTTP.OK) {
        dispatch({
          type: types.REFRESH_TUTOR_STUDENTS,
          data: res.body
        })
      }
    })()
  }
}

export const getProgramById = (programId) => {
  return (dispatch) => {
    (async () => {
      const res = await request.get(`./api/programs/${programId}`)
      if (res.status === HTTP.OK) {
        dispatch({
          type: types.REFRESH_PROGRAM,
          data: res.body
        })
        dispatch(searchStudentsByProgramId(programId))
      }
    })()
  }
}

export const follow = (programId, tutorId, studentIds) => {
  return (dispatch) => {
    (async () => {
      const res = await request.post(`./api/programs/${programId}/tutors/${tutorId}/students/${studentIds}`)
      if (res.status === HTTP.CREATED) {
        message.success(convertContent('添加成功'))
        dispatch(getMyStudents(tutorId, programId))
      }
    })()
  }
}
export const unFollow = (programId, tutorId, studentIds) => {
  return (dispatch) => {
    (async () => {
      const res = await request.del(`./api/programs/${programId}/tutors/${tutorId}/students/${studentIds}`)
      if (res.status === HTTP.NO_CONTENT) {
        message.success(convertContent('移除成功'))
        dispatch(getMyStudents(tutorId, programId))
      }
    })()
  }
}
export const removeStudentFromProgram = (id, programId) => {
  return (dispatch) => {
    (async () => {
      const res = await request.del(`./api/programs/${programId}/students/${id}`)
      if (res.status === HTTP.NO_CONTENT) {
        message.success(convertContent('移除成功'))
        dispatch(searchStudentsByProgramId(programId))
      }
    })()
  }
}
export const removeTutorFromProgram = (id, programId) => {
  return (dispatch) => {
    (async () => {
      const res = await request.del(`./api/programs/${programId}/tutors/${id}`)
      if (res.status === HTTP.NO_CONTENT) {
        message.success(convertContent('移除成功'))
        dispatch(searchStudentsByProgramId(programId))
      }
    })()
  }
}
export const getCurrentOrgProgramsBasicQuizzes = () => {
  return (dispatch) => {
    (async () => {
      const res = await request.get(`./api/v2/programs/basicQuizzes`)
      if (res.status === HTTP.OK) {
        dispatch(dispatch({
          type: types.GET_CURRENT_ORG_PROGRAMS_BASIC_QUIZZES,
          data: res.body
        }))
      }
    })()
  }
}
export const calculateScore = (taskId, assignmentId) => {
  return () => {
    (async () => {
      const res = await request.update(`./api/v2/programs/tasks/${taskId}/assignments/${assignmentId}/score`)
      if (res.status === HTTP.NO_CONTENT) {
        message.success('更新成功')
      }
    })()
  }
}
